/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JTicketStatePanel.java
 *
 * Created on 2009-8-13, 9:17:09
 */

package com.openbravo.pos.sales;

import cn.ekuma.epos.bean.util.sale.TicketHelper;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.bean.sales.TicketLine;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class JTicketStatePanel extends javax.swing.JPanel {
    //private final PaymentsTableModel paymentsTableModel;
     PropListModel propListModel;


    /** Creates new form JTicketStatePanel */
    public JTicketStatePanel() {
        initComponents();
        propListModel=new PropListModel(null);
        jList1.setModel(propListModel);
        //paymentsTableModel=new PaymentsTableModel();
        //jTable1.setModel(paymentsTableModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jTicketType = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jNumTotal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPriceTotal = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPriceTxtTotal = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPriceAndTxtTotal = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTicketRate = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jDefaultPriceTotal = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jDefaultPriceTxtTotal = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jDefaultPriceAndTxtTotal = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPrerNumTotal = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jPrerPriceTotal = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setFocusable(false);
        setRequestFocusEnabled(false);
        setVerifyInputWhenFocusTarget(false);
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jPanel1.setMaximumSize(new java.awt.Dimension(150, 2147483647));
        jPanel1.setMinimumSize(new java.awt.Dimension(150, 361));
        jPanel1.setPreferredSize(new java.awt.Dimension(88, 409));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel6.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 255, 204), 1, true));
        jPanel6.setLayout(new java.awt.GridLayout(1, 2));

        jLabel11.setText(AppLocal.getIntString("label.statePanel_ticketType")); // NOI18N
        jPanel6.add(jLabel11);

        jTicketType.setEditable(false);
        jTicketType.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTicketType.setText("收银");
        jTicketType.setFocusable(false);
        jTicketType.setRequestFocusEnabled(false);
        jTicketType.setVerifyInputWhenFocusTarget(false);
        jPanel6.add(jTicketType);

        jPanel1.add(jPanel6);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.statePanel_title_end"))); // NOI18N
        jPanel2.setEnabled(false);
        jPanel2.setFocusable(false);
        jPanel2.setMaximumSize(new java.awt.Dimension(180, 120));
        jPanel2.setMinimumSize(new java.awt.Dimension(180, 100));
        jPanel2.setPreferredSize(new java.awt.Dimension(88, 100));
        jPanel2.setRequestFocusEnabled(false);
        jPanel2.setVerifyInputWhenFocusTarget(false);
        jPanel2.setLayout(new java.awt.GridLayout(4, 2, 1, 1));

        jLabel1.setText(AppLocal.getIntString("label.statePanel_totalNum")); // NOI18N
        jLabel1.setFocusable(false);
        jLabel1.setInheritsPopupMenu(false);
        jLabel1.setRequestFocusEnabled(false);
        jLabel1.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jLabel1);

        jNumTotal.setEditable(false);
        jNumTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jNumTotal.setFocusable(false);
        jNumTotal.setRequestFocusEnabled(false);
        jNumTotal.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jNumTotal);

        jLabel2.setText(AppLocal.getIntString("label.statePanel_totalSubPrice")); // NOI18N
        jLabel2.setFocusable(false);
        jLabel2.setInheritsPopupMenu(false);
        jLabel2.setRequestFocusEnabled(false);
        jLabel2.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jLabel2);

        jPriceTotal.setEditable(false);
        jPriceTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPriceTotal.setFocusable(false);
        jPriceTotal.setRequestFocusEnabled(false);
        jPriceTotal.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jPriceTotal);

        jLabel3.setText(AppLocal.getIntString("label.statePanel_totalTaxPrice")); // NOI18N
        jLabel3.setFocusable(false);
        jLabel3.setInheritsPopupMenu(false);
        jLabel3.setRequestFocusEnabled(false);
        jLabel3.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jLabel3);

        jPriceTxtTotal.setEditable(false);
        jPriceTxtTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPriceTxtTotal.setFocusable(false);
        jPriceTxtTotal.setRequestFocusEnabled(false);
        jPriceTxtTotal.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jPriceTxtTotal);

        jLabel10.setText(AppLocal.getIntString("label.statePanel_totalPrice")); // NOI18N
        jLabel10.setFocusable(false);
        jLabel10.setInheritsPopupMenu(false);
        jLabel10.setRequestFocusEnabled(false);
        jLabel10.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jLabel10);

        jPriceAndTxtTotal.setEditable(false);
        jPriceAndTxtTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPriceAndTxtTotal.setFocusable(false);
        jPriceAndTxtTotal.setRequestFocusEnabled(false);
        jPriceAndTxtTotal.setVerifyInputWhenFocusTarget(false);
        jPanel2.add(jPriceAndTxtTotal);

        jPanel1.add(jPanel2);

        jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 255, 204), 1, true));
        jPanel3.setFocusable(false);
        jPanel3.setRequestFocusEnabled(false);
        jPanel3.setVerifyInputWhenFocusTarget(false);
        jPanel3.setLayout(new java.awt.GridLayout(1, 2));

        jLabel4.setText(AppLocal.getIntString("label.statePanel_rate")); // NOI18N
        jLabel4.setFocusable(false);
        jLabel4.setInheritsPopupMenu(false);
        jLabel4.setRequestFocusEnabled(false);
        jLabel4.setVerifyInputWhenFocusTarget(false);
        jPanel3.add(jLabel4);

        jTicketRate.setEditable(false);
        jTicketRate.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTicketRate.setFocusable(false);
        jTicketRate.setRequestFocusEnabled(false);
        jTicketRate.setVerifyInputWhenFocusTarget(false);
        jPanel3.add(jTicketRate);

        jPanel1.add(jPanel3);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.statePanel_title_before"))); // NOI18N
        jPanel5.setFocusable(false);
        jPanel5.setRequestFocusEnabled(false);
        jPanel5.setVerifyInputWhenFocusTarget(false);
        jPanel5.setLayout(new java.awt.GridLayout(3, 2));

        jLabel7.setText(AppLocal.getIntString("label.statePanel_totalSubPrice")); // NOI18N
        jLabel7.setFocusable(false);
        jLabel7.setInheritsPopupMenu(false);
        jLabel7.setRequestFocusEnabled(false);
        jLabel7.setVerifyInputWhenFocusTarget(false);
        jPanel5.add(jLabel7);

        jDefaultPriceTotal.setEditable(false);
        jDefaultPriceTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jDefaultPriceTotal.setFocusable(false);
        jDefaultPriceTotal.setRequestFocusEnabled(false);
        jDefaultPriceTotal.setVerifyInputWhenFocusTarget(false);
        jPanel5.add(jDefaultPriceTotal);

        jLabel8.setText(AppLocal.getIntString("label.statePanel_totalTaxPrice")); // NOI18N
        jLabel8.setFocusable(false);
        jLabel8.setInheritsPopupMenu(false);
        jLabel8.setRequestFocusEnabled(false);
        jLabel8.setVerifyInputWhenFocusTarget(false);
        jPanel5.add(jLabel8);

        jDefaultPriceTxtTotal.setEditable(false);
        jDefaultPriceTxtTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jDefaultPriceTxtTotal.setFocusable(false);
        jDefaultPriceTxtTotal.setRequestFocusEnabled(false);
        jDefaultPriceTxtTotal.setVerifyInputWhenFocusTarget(false);
        jPanel5.add(jDefaultPriceTxtTotal);

        jLabel9.setText(AppLocal.getIntString("label.statePanel_totalPrice")); // NOI18N
        jLabel9.setFocusable(false);
        jLabel9.setInheritsPopupMenu(false);
        jLabel9.setRequestFocusEnabled(false);
        jLabel9.setVerifyInputWhenFocusTarget(false);
        jPanel5.add(jLabel9);

        jDefaultPriceAndTxtTotal.setEditable(false);
        jDefaultPriceAndTxtTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jDefaultPriceAndTxtTotal.setFocusable(false);
        jDefaultPriceAndTxtTotal.setRequestFocusEnabled(false);
        jDefaultPriceAndTxtTotal.setVerifyInputWhenFocusTarget(false);
        jPanel5.add(jDefaultPriceAndTxtTotal);

        jPanel1.add(jPanel5);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.statePanel_title_pref"))); // NOI18N
        jPanel4.setFocusable(false);
        jPanel4.setRequestFocusEnabled(false);
        jPanel4.setVerifyInputWhenFocusTarget(false);
        jPanel4.setLayout(new java.awt.GridLayout(2, 2));

        jLabel5.setText(AppLocal.getIntString("label.statePanel_prefNum")); // NOI18N
        jLabel5.setFocusable(false);
        jLabel5.setInheritsPopupMenu(false);
        jLabel5.setRequestFocusEnabled(false);
        jLabel5.setVerifyInputWhenFocusTarget(false);
        jPanel4.add(jLabel5);

        jPrerNumTotal.setEditable(false);
        jPrerNumTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPrerNumTotal.setFocusable(false);
        jPrerNumTotal.setRequestFocusEnabled(false);
        jPrerNumTotal.setVerifyInputWhenFocusTarget(false);
        jPanel4.add(jPrerNumTotal);

        jLabel6.setText(AppLocal.getIntString("label.statePanel_prefPrice")); // NOI18N
        jLabel6.setFocusable(false);
        jLabel6.setInheritsPopupMenu(false);
        jLabel6.setRequestFocusEnabled(false);
        jLabel6.setVerifyInputWhenFocusTarget(false);
        jPanel4.add(jLabel6);

        jPrerPriceTotal.setEditable(false);
        jPrerPriceTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPrerPriceTotal.setFocusable(false);
        jPrerPriceTotal.setRequestFocusEnabled(false);
        jPrerPriceTotal.setVerifyInputWhenFocusTarget(false);
        jPanel4.add(jPrerPriceTotal);

        jPanel1.add(jPanel4);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(AppLocal.getIntString("label.statePanel_title_prep"))); // NOI18N
        jPanel7.setEnabled(false);
        jPanel7.setFocusable(false);
        jPanel7.setRequestFocusEnabled(false);
        jPanel7.setVerifyInputWhenFocusTarget(false);
        jPanel7.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setFocusable(false);
        jScrollPane1.setRequestFocusEnabled(false);
        jScrollPane1.setVerifyInputWhenFocusTarget(false);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setFocusable(false);
        jList1.setRequestFocusEnabled(false);
        jList1.setVerifyInputWhenFocusTarget(false);
        jScrollPane1.setViewportView(jList1);

        jPanel7.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel7);

        add(jPanel1);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jDefaultPriceAndTxtTotal;
    private javax.swing.JTextField jDefaultPriceTotal;
    private javax.swing.JTextField jDefaultPriceTxtTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JTextField jNumTotal;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JTextField jPrerNumTotal;
    private javax.swing.JTextField jPrerPriceTotal;
    private javax.swing.JTextField jPriceAndTxtTotal;
    private javax.swing.JTextField jPriceTotal;
    private javax.swing.JTextField jPriceTxtTotal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTicketRate;
    private javax.swing.JTextField jTicketType;
    // End of variables declaration//GEN-END:variables

    public void refreshTicket(Ticket m_oTicket) {
        if(m_oTicket==null){
            jDefaultPriceAndTxtTotal.setText(null);
            jDefaultPriceTotal.setText(null);
            jDefaultPriceTxtTotal.setText(null);
            jNumTotal.setText(null);
            jPrerNumTotal.setText(null);
            jPrerPriceTotal.setText(null);
            jPriceAndTxtTotal.setText(null);
            jPriceTotal.setText(null);
            jPriceTxtTotal.setText(null);
            jTicketRate.setText(null);
            jTicketType.setText(null);
            //paymentsTableModel.reset();
        }else{
            jDefaultPriceAndTxtTotal.setText(TicketHelper.printDefTotal(m_oTicket));
            jDefaultPriceTotal.setText(TicketHelper.printDefSubTotal(m_oTicket));
            jDefaultPriceTxtTotal.setText(TicketHelper.printDefTax(m_oTicket));
            jNumTotal.setText(TicketHelper.printArticlesCount(m_oTicket));
            jPrerNumTotal.setText(TicketHelper.printPrefArticlesCount(m_oTicket));
            jPrerPriceTotal.setText(TicketHelper.printPrefSubTotal(m_oTicket));
            jPriceAndTxtTotal.setText(TicketHelper.printTotal(m_oTicket));
            jPriceTotal.setText(TicketHelper.printSubTotal(m_oTicket));
            jPriceTxtTotal.setText(TicketHelper.printTax(m_oTicket));
            jTicketRate.setText(TicketHelper.printPriceRate(m_oTicket));
            if(Ticket.RECEIPT_NORMAL==m_oTicket.getTicketType()){
                jTicketType.setText(AppLocal.getIntString("button.sales"));
            }else if(Ticket.RECEIPT_REFUND==m_oTicket.getTicketType()){
                 jTicketType.setText(AppLocal.getIntString("button.refund"));
            }
            //paymentsTableModel.setPayments(m_oTicket.getPayments());
        }
    }

   public void refreshTicketLine(TicketLine oLine) {
       String ticketLineExtProp=oLine.getProperty(TicketLine.TICKETlINE_EXTPROP, "");
       propListModel.reset(splitProp(ticketLineExtProp));
   }

    private ArrayList<String> splitProp(String prop){
       ArrayList<String> propList=new ArrayList<String>();
       if(prop==null||prop.isEmpty())
           return propList;
       String[] propEntry=prop.split(",");
       for(int i=0;i<propEntry.length;i++)
           propList.add(propEntry[i]);
       return propList;
   }


    private class PropListModel extends javax.swing.AbstractListModel{
       ArrayList<String> propList;

       public PropListModel(ArrayList<String> inPropList){
           if(inPropList==null)
               propList=new ArrayList();
           else
               propList=inPropList;
       }

        @Override
        public int getSize() {
            return propList.size();
        }

        @Override
        public Object getElementAt(int index) {
           return propList.get(index);
        }

        public void addRow(String oLine) {
            insertRow(propList.size(), oLine);
        }

        public void insertRow(int index, String oLine) {
            propList.add(index, oLine);
            fireIntervalAdded(this, index, index);
        }

        public void removeRow(int row) {
            propList.remove(row);
            fireIntervalRemoved(this, row, row);
        }

        public ArrayList getList(){
            return propList;
        }

        public void addRow(ArrayList rows){
            propList.addAll(rows);
        }

        public void reset(){
            propList=new ArrayList();
        }

        public void reset(ArrayList rows){
            propList=rows;
            //if(rows.size()>0)
            fireContentsChanged(this, 0, rows.size()-1);
        }

        public String buildProps(){
            String retString="";
            for(int i=0;i<propList.size();i++)
                retString=retString+","+propList.get(i).toString();
            return retString;
        }
    }

  /*
    class PaymentsTableModel extends javax.swing.table.AbstractTableModel{
        List<PaymentInfo> payments;

        PaymentsTableModel(){
            payments=new ArrayList<PaymentInfo>();
        }

        public void setPayments(List<PaymentInfo> paymen) {
            payments = paymen;
            fireTableDataChanged();
        }

        public void reset(){
            payments=new ArrayList<PaymentInfo>();
            fireTableDataChanged();
        }

        @Override
        public int getRowCount() {
            return payments.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch(columnIndex){
                case 0:
                    return payments.get(rowIndex).getName();
                case 1:
                    return payments.get(rowIndex).printTotal();
            }
            return null;
        }

    }
  */
}
